package com.zjz.controller;

import com.zjz.pojo.Books;
import com.zjz.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.awt.print.Book;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    // 掉service层

    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    // 查询全部📕，并且返回到一个书籍展示页面
    @RequestMapping("/allbook")
    public String list(Model model){
        List<Books> list = bookService.queryAllBook();

        model.addAttribute("list",list);
        return "AllBook";
    }

    // 去增加页面
    @RequestMapping("/toAddBook")
    public String toAddBook(){


        return "addBook";
    }

    // 增加页面
    @RequestMapping("/addBook")
    public String addBook(Books books){

        bookService.addBook(books);

        return "redirect:/book/allbook";  // 重定向到我们的@RequestMapping("/allbook")请求
    }



    @RequestMapping("/toUpdateBook")
    public String toUpdateBook(int id,Model model){
        Books books = bookService.queryBookById(id);

        model.addAttribute("QBooks",books);
        return "updateBook";
    }


    @RequestMapping("/update")
    public String update(Books books){
        bookService.updateBook(books);
        return "redirect:/book/allbook";
    }


    @RequestMapping("/deleteBook/{bookID}")
    public String deleteBook(@PathVariable("bookID") int id, Model model){
        bookService.deleteBookById(id);
//        model.addAttribute("msgdelete","删除成功!");
        return "redirect:/book/allbook";
    }


    // 查询书籍
    @RequestMapping("/queryBook")
    public String queryBook(String queryBookName,Model model){
        List<Books> list = bookService.queryBookByName("%"+queryBookName+"%");


        if(list.isEmpty()){
            list=bookService.queryAllBook();
            model.addAttribute("error","未查到");

        }

        model.addAttribute("list",list);

        return "AllBook";

    }

}
